//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "VarianceGammaModel.h"
using namespace Cephei::QL::Experimental::Variancegamma;
#include <gen/QL/Experimental/Variancegamma/VarianceGammaProcess.h>
#include <gen/QL/Models/CalibrationHelper.h>
#include <gen/QL/Math/Optimization/OptimizationMethod.h>
#include <gen/QL/Math/Optimization/EndCriteria.h>
#include <gen/QL/Math/Optimization/Constraint.h>
#include <gen/QL/Math/Array.h>
#include <gen/QL/Models/CalibratedModel.h>
using namespace Cephei::QL::Models;
using namespace Cephei::QL::Math::Optimization;
using namespace Cephei::QL::Math;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Variancegamma::CVarianceGammaModel::CVarianceGammaModel (Cephei::QL::Experimental::Variancegamma::IVarianceGammaProcess^ process) : CCalibratedModel(CVarianceGammaModel::typeid)
{
    CVarianceGammaProcess^ _Cprocess;
    try
    {
#ifdef HANDLE
        _phVarianceGammaModel = NULL;
#endif
        _Cprocess = safe_cast<CVarianceGammaProcess^> (process);
        _Cprocess->Lock();
        boost::shared_ptr<QuantLib::VarianceGammaProcess>& _process = static_cast<boost::shared_ptr<QuantLib::VarianceGammaProcess>&> (_Cprocess->GetShared ()); 
        _ppVarianceGammaModel = new boost::shared_ptr<QuantLib::VarianceGammaModel> (new QuantLib::VarianceGammaModel ( _process ));
        SetCalibratedModel (boost::dynamic_pointer_cast<QuantLib::CalibratedModel> (*_ppVarianceGammaModel));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
    }
}
Cephei::QL::Experimental::Variancegamma::CVarianceGammaModel::CVarianceGammaModel (boost::shared_ptr<QuantLib::VarianceGammaModel>& childNative, Object^ owner) : CCalibratedModel(CVarianceGammaModel::typeid)
{
#ifdef HANDLE
	_phVarianceGammaModel = NULL;
#endif
	_ppVarianceGammaModel = &childNative;
    _ppCalibratedModel = new boost::shared_ptr<QuantLib::CalibratedModel> (boost::dynamic_pointer_cast<QuantLib::CalibratedModel> (*_ppVarianceGammaModel));
}
Cephei::QL::Experimental::Variancegamma::CVarianceGammaModel::CVarianceGammaModel (QuantLib::VarianceGammaModel& childNative, Object^ owner) : CCalibratedModel(CVarianceGammaModel::typeid)
{
#ifdef HANDLE
	_phVarianceGammaModel = NULL;
#endif
	_ppVarianceGammaModel = new boost::shared_ptr<QuantLib::VarianceGammaModel> (&childNative);
    _ppCalibratedModel = new boost::shared_ptr<QuantLib::CalibratedModel> (boost::dynamic_pointer_cast<QuantLib::CalibratedModel> (*_ppVarianceGammaModel));
    _VarianceGammaModelOwner = owner;
    _CalibratedModelOwner = owner;
}

Cephei::QL::Experimental::Variancegamma::CVarianceGammaModel::CVarianceGammaModel (CVarianceGammaModel^ copy) : CCalibratedModel(CVarianceGammaModel::typeid)
{
#ifdef HANDLE
	_phVarianceGammaModel = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppVarianceGammaModel = new boost::shared_ptr<QuantLib::VarianceGammaModel> (copy->GetShared());
        _ppCalibratedModel = new boost::shared_ptr<QuantLib::CalibratedModel> (boost::dynamic_pointer_cast<QuantLib::CalibratedModel> (*_ppVarianceGammaModel));
    }
}
Cephei::QL::Experimental::Variancegamma::CVarianceGammaModel::CVarianceGammaModel (PLATFORM::Type^ t) : CCalibratedModel(CVarianceGammaModel::typeid)
{
#ifdef HANDLE
	_phVarianceGammaModel = NULL;
#endif
	if (!t->IsSubclassOf(CVarianceGammaModel::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Variancegamma::CVarianceGammaModel::CVarianceGammaModel (QuantLib::Handle<QuantLib::VarianceGammaModel>& childNative, Object^ owner)  : CCalibratedModel(CVarianceGammaModel::typeid)
{
	_phVarianceGammaModel = &childNative;
	_ppVarianceGammaModel = &static_cast<boost::shared_ptr<QuantLib::VarianceGammaModel>>(childNative.currentLink());
    _ppCalibratedModel = new boost::shared_ptr<QuantLib::CalibratedModel> (boost::dynamic_pointer_cast<QuantLib::CalibratedModel> (*_ppVarianceGammaModel));
    _VarianceGammaModelOwner = owner;
}
Cephei::QL::Experimental::Variancegamma::CVarianceGammaModel::CVarianceGammaModel (QuantLib::Handle<QuantLib::VarianceGammaModel> childNative)  : CCalibratedModel(CVarianceGammaModel::typeid)
{
	_phVarianceGammaModel = &childNative;
	_ppVarianceGammaModel = &static_cast<boost::shared_ptr<QuantLib::VarianceGammaModel>>(childNative.currentLink());
    _ppCalibratedModel = new boost::shared_ptr<QuantLib::CalibratedModel> (boost::dynamic_pointer_cast<QuantLib::CalibratedModel> (*_ppVarianceGammaModel));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Variancegamma::CVarianceGammaModel::CVarianceGammaModel (QuantLib::VarianceGammaModel childNative)  : CCalibratedModel(CVarianceGammaModel::typeid)
{
#ifdef HANDLE
	_phVarianceGammaModel = NULL;
#endif
	_ppVarianceGammaModel = new boost::shared_ptr<QuantLib::VarianceGammaModel> (new QuantLib::VarianceGammaModel (childNative));
    _ppCalibratedModel = new boost::shared_ptr<QuantLib::CalibratedModel> (boost::dynamic_pointer_cast<QuantLib::CalibratedModel> (*_ppVarianceGammaModel));
}
#endif

Cephei::QL::Experimental::Variancegamma::CVarianceGammaModel::~CVarianceGammaModel ()
{
    if (_ppVarianceGammaModel != NULL)
    {
	    delete _ppVarianceGammaModel;
        _ppVarianceGammaModel = NULL;
    }
}
Cephei::QL::Experimental::Variancegamma::CVarianceGammaModel::!CVarianceGammaModel ()
{
    if (_ppVarianceGammaModel != NULL)
    {
	    delete _ppVarianceGammaModel;
    }
}
QuantLib::VarianceGammaModel& Cephei::QL::Experimental::Variancegamma::CVarianceGammaModel::GetReference ()
{
    if (_ppVarianceGammaModel == NULL) throw REFNEW NativeNullException ();
	return **_ppVarianceGammaModel;
}
boost::shared_ptr<QuantLib::VarianceGammaModel>& Cephei::QL::Experimental::Variancegamma::CVarianceGammaModel::GetShared ()
{
    if (_ppVarianceGammaModel == NULL) throw REFNEW NativeNullException ();
	return *_ppVarianceGammaModel;
}
QuantLib::VarianceGammaModel* Cephei::QL::Experimental::Variancegamma::CVarianceGammaModel::GetPointer ()
{
    if (_ppVarianceGammaModel == NULL) throw REFNEW NativeNullException ();
	return &**_ppVarianceGammaModel;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::VarianceGammaModel>& Cephei::QL::Experimental::Variancegamma::CVarianceGammaModel::GetHandle ()
{
	if (_phVarianceGammaModel == NULL)
	{
		_phVarianceGammaModel = new Handle<QuantLib::VarianceGammaModel> (*_ppVarianceGammaModel);
	}
	return *_phVarianceGammaModel;
}
#endif
bool Cephei::QL::Experimental::Variancegamma::CVarianceGammaModel::HasNative () 
{
	return (_ppVarianceGammaModel != NULL);
}

Double Cephei::QL::Experimental::Variancegamma::CVarianceGammaModel::Nu::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppVarianceGammaModel)->nu ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Variancegamma::IVarianceGammaProcess^ Cephei::QL::Experimental::Variancegamma::CVarianceGammaModel::Process::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::VarianceGammaProcess> _rv = (boost::shared_ptr<QuantLib::VarianceGammaProcess>)(*_ppVarianceGammaModel)->process ( );   
        Cephei::QL::Experimental::Variancegamma::CVarianceGammaProcess^ _nrv = REFNEW Cephei::QL::Experimental::Variancegamma::CVarianceGammaProcess (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Variancegamma::CVarianceGammaModel::Sigma::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppVarianceGammaModel)->sigma ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Variancegamma::CVarianceGammaModel::Theta::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppVarianceGammaModel)->theta ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Variancegamma::IVarianceGammaModel^ Cephei::QL::Experimental::Variancegamma::CVarianceGammaModel_Factory::Create (Cephei::QL::Experimental::Variancegamma::IVarianceGammaProcess^ process)
{
    return REFNEW CVarianceGammaModel ( process);
}
